package com.spzx.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.domain.ProductSpec;
import com.spzx.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/ProductSpec")
@Tag(name = "商品规格管理接口")
public class ProductSpecController extends BaseController {
    @Resource
    ProductSpecService productSpecService;

    @GetMapping("/list")
    @Operation(summary = "分页商品规格列表")
    public TableDataInfo listProductSpec(ProductSpec productSpec) {
        startPage();
        List<ProductSpec> productSpecs = productSpecService.listProductSpec(productSpec);
        return getDataTable(productSpecs);
    }

    @GetMapping("/{id}")
    @Operation(summary = "商品规格详细信息")
    public AjaxResult getDetails(@PathVariable("id") Long id) {
        ProductSpec details = productSpecService.getDetails(id);
        return AjaxResult.success(details);
    }

    @PostMapping
    @Operation(summary = "新增商品规格")
    public AjaxResult insert(@RequestBody @Validated ProductSpec productSpec) {
        boolean save = productSpecService.saveProductSpec(productSpec);
        return toAjax(save);
    }

    @PutMapping
    @Operation(summary = "修改商品规格")
    public AjaxResult update(@RequestBody @Validated ProductSpec productSpec) {
        return toAjax(productSpecService.updateProductSpec(productSpec));
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品规格")
    public AjaxResult delete(@PathVariable ("id") Long[] id ){
        boolean b = productSpecService.removeBatchByIds(Arrays.stream(id).toList());
        return toAjax(b);
    }


}
